/**
 * Copyright (c) subnew
 * Licensed under the GPL;
 * @email:joseanluo@gmail.com
 * @Title: BrowserUtils.java
 * @Package com.subnew.copyman.frame.utils.browser
 */
package com.subnew.mango.frame.utils.browser;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import com.subnew.mango.frame.constant.BrowserType;


/**
 * 浏览器工具类.
 *
 * @author JoseanLuo
 * @date 2014年9月2日 上午8:54:27
 * @version V1.0
 */
public class BrowserUtil {
	private final static String IE11 = "rv:11.0";
	private final static String IE10 = "MSIE 10.0";
	private final static String IE9 = "MSIE 9.0";
	private final static String IE8 = "MSIE 8.0";
	private final static String IE7 = "MSIE 7.0";
	private final static String IE6 = "MSIE 6.0";
	private final static String MAXTHON = "Maxthon";
	private final static String QQ = "QQBrowser";
	private final static String GREEN = "GreenBrowser";
	private final static String SE360 = "360SE";
	private final static String FIREFOX = "Firefox";
	private final static String OPERA = "Opera";
	private final static String CHROME = "Chrome";
	private final static String SAFARI = "Safari";
	private final static String CAMINO = "Camino";
	private final static String OTHER = "其它";

	/**
	 * 
	 * 判断是否是IE浏览器
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @return 是刚返回 true,否则false
	 * @Title: isIE
	 * @author JoseanLuo
	 * @date 2014年9月2日 上午9:06:03
	 * @version V1.0
	 */
	public static boolean isIE(HttpServletRequest request) {
		return (getUserAgent(request).indexOf("msie") > 0 || getUserAgent(request).indexOf("rv:11.0") > 0) ? true : false;
	}

	/**
	 * 获取浏览器类型
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @return 返回客户浏览器类型。
	 * @Title: getBrowserType
	 * @author JoseanLuo
	 * @date 2014年9月2日 上午9:20:44
	 * @version V1.0
	 */
	public static BrowserType getBrowserType(HttpServletRequest request) {
		BrowserType browserType = null;
		if (getBrowserType(request, IE10)) {
			browserType = BrowserType.IE10;
		} else if (getBrowserType(request, IE11)) {
			browserType = BrowserType.IE11;
		} else if (getBrowserType(request, IE6)) {
			browserType = BrowserType.IE6;
		} else if (getBrowserType(request, IE7)) {
			browserType = BrowserType.IE7;
		} else if (getBrowserType(request, IE8)) {
			browserType = BrowserType.IE8;
		} else if (getBrowserType(request, IE9)) {
			browserType = BrowserType.IE9;
		} else if (getBrowserType(request, FIREFOX)) {
			browserType = BrowserType.Firefox;
		} else if (getBrowserType(request, MAXTHON)) {
			browserType = BrowserType.Maxthon;
		} else if (getBrowserType(request, QQ)) {
			browserType = BrowserType.QQBrowser;
		} else if (getBrowserType(request, GREEN)) {
			browserType = BrowserType.GreenBrowser;
		} else if (getBrowserType(request, SE360)) {
			browserType = BrowserType.SE360;
		} else if (getBrowserType(request, OPERA)) {
			browserType = BrowserType.Opera;
		} else if (getBrowserType(request, CHROME)) {
			browserType = BrowserType.Chrome;
		} else if (getBrowserType(request, SAFARI)) {
			browserType = BrowserType.Safari;
		} else if (getBrowserType(request, CAMINO)) {
			browserType = BrowserType.Camino;
		} else {
			browserType = BrowserType.Others;
		}

		return browserType;
	}

	/**
	 * 
	 * 判断用户浏览器类型
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @return 返回对应浏览器类型，没有枚举的刚返回其它。
	 * @Title: checkBrowse
	 * @author JoseanLuo
	 * @date 2014年9月2日 上午9:48:36
	 * @version V1.0
	 */
	public static String checkBrowse(HttpServletRequest request) {
		String userAgent = getUserAgent(request);
		if (regex(OPERA, userAgent))
			return OPERA;
		if (regex(CHROME, userAgent))
			return CHROME;
		if (regex(FIREFOX, userAgent))
			return FIREFOX;
		if (regex(SAFARI, userAgent))
			return SAFARI;
		if (regex(SE360, userAgent))
			return SE360;
		if (regex(GREEN, userAgent))
			return GREEN;
		if (regex(QQ, userAgent))
			return QQ;
		if (regex(MAXTHON, userAgent))
			return MAXTHON;
		if (regex(IE11, userAgent))
			return IE11;
		if (regex(IE10, userAgent))
			return IE10;
		if (regex(IE9, userAgent))
			return IE9;
		if (regex(IE8, userAgent))
			return IE8;
		if (regex(IE7, userAgent))
			return IE7;
		if (regex(IE6, userAgent))
			return IE6;
		return OTHER;

	}

	/**
	 * 
	 * 正则匹配是否是对应浏览器类型。
	 * 
	 * @param regex
	 *            正则式
	 * @param userAgent
	 *            request‘s userAgent
	 * @return 是返回true.
	 * @Title: regex
	 * @author JoseanLuo
	 * @date 2014年9月2日 上午9:42:01
	 * @version V1.0
	 */
	public static boolean regex(String regex, String userAgent) {
		Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
		Matcher matcher = pattern.matcher(userAgent);
		return matcher.find();
	}

	/**
	 * 
	 * 判断是是否是指定的brosertype（浏览器类型），
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @param brosertype
	 *            浏览器类型
	 * @return 是则返回true,否则false.
	 * @Title: getBrowserType
	 * @author JoseanLuo
	 * @date 2014年9月2日 上午9:17:21
	 * @version V1.0
	 */
	private static boolean getBrowserType(HttpServletRequest request, String brosertype) {
		return getUserAgent(request).indexOf(brosertype) > 0 ? true : false;
	}

	/**
	 * 获取客户端信息,取得用户USER-AGENT.
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @return the request header user-agent.
	 */
	public static String getUserAgent(HttpServletRequest request) {
		return request.getHeader("USER-AGENT");
	}


    public static String getIpAddr(HttpServletRequest request) {
        if (request == null) {
            return "unknown";
        }
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Forwarded-For");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
